package com.sxz.sbs.module.order;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.order.adapter.OrderListAdapter;
import com.sxz.sbs.module.order.mode.IntentOrder;
import com.sxz.sbs.module.order.mode.OrderhandleBean;
import com.sxz.sbs.module.order.presenter.OrderListPresenter;
import com.sxz.sbs.module.order.view.OrderListView;
import com.sxz.sbs.widget.PhoneDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 订单搜索结果
 *
 * @author tian
 */
public class OrderSearchActivity extends BaseActivity<OrderListPresenter> implements OrderListView {

    @BindView(R.id.cet_search_order)
    ClearEditText cetSearchOrder;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    public static final String SEARCH_ORDER_KEY = "search_order_key";
    private String keyWord;
    private OrderListAdapter allHousesAdapter;
    private List<OrderhandleBean.ListBean> list=new ArrayList<>();
    private int currentPage = 1;
    @Override
    protected void init() {
        keyWord = getIntent().getStringExtra(SEARCH_ORDER_KEY);
        cetSearchOrder.setText(keyWord);
        cetSearchOrder.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    finish();
                } else {

                }
            }
        });
        cetSearchOrder.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    InputMethodManager imm = (InputMethodManager) v.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isActive())
                        imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    getOrderList();
                    return true;
                }
                return false;
            }
        });
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setRefreshing(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        allHousesAdapter=new OrderListAdapter(list);
        recyclerView.setAdapter(allHousesAdapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getOrderList();
            }
        });
        allHousesAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getOrderList();
            }
        },recyclerView);

        allHousesAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(OrderSearchActivity.this,OrderDetailActivity.class);
                intent.putExtra("order_id",list.get(position).getId());
                startActivity(intent);
            }
        });
        allHousesAdapter.setOnOrderClickListener(new OrderListAdapter.OnOrderClickListener() {
            @Override
            public void onAllotOrder(OrderhandleBean.ListBean item) {
                IntentOrder intentOrder = new IntentOrder();
                intentOrder.setOrderId(item.getId());
                intentOrder.setParkingId(item.getParkingId());
                startActivity(new Intent(OrderSearchActivity.this,AllotOrderActivity.class)
                        .putExtra("intent_order",intentOrder));
            }

            @Override
            public void onCallOrder(String phone) {
                PhoneDialog.callPhone(OrderSearchActivity.this,phone);
            }

            @Override
            public void onModifyOrder(OrderhandleBean.ListBean item) {
                IntentOrder intentOrder1 = new IntentOrder();
                intentOrder1.setOrderId(item.getId());
                intentOrder1.setIsByStage(item.getIsByStage());
                intentOrder1.setPrice(item.getDealPrice() + "");
                intentOrder1.setStatusInt(item.getStatus());
                intentOrder1.setRemark(item.getRemark());
                startActivity(new Intent(OrderSearchActivity.this,OrderModifyActivity.class)
                        .putExtra("intent_order",intentOrder1));
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getOrderList();
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
        currentPage = 1;
        getOrderList();
    }
    private void getOrderList() {
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("status", -1);
        map.put("pageNumber",currentPage);
        map.put("pageSize", 10);
        map.put("keyword", keyWord);
        mPresenter.getOrderList(map);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_search;
    }

    @Override
    protected OrderListPresenter createPresenter() {
        return new OrderListPresenter();
    }

    @OnClick(R.id.iv_title_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onOrderListSuccess(OrderhandleBean data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data.getList() != null && data.getList().size() >0) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    list.clear();
                }
                list.addAll(data.getList());
                allHousesAdapter.notifyDataSetChanged();
                allHousesAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    list.clear();
                    allHousesAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    allHousesAdapter.loadMoreEnd();
                    allHousesAdapter.notifyDataSetChanged();
                }
            }
        }
    }
    @Override
    public void showError(int errorTag, String msg) {
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showEmpty();
        }
    }
}
